# -*- coding: utf-8 -*-
"""
TG Trader Tools 菜单组件
"""
from PySide6.QtWidgets import QWidget, QVBoxLayout, QListWidget, QListWidgetItem, QLabel, QFrame
from PySide6.QtCore import Qt, Signal

from ..base import TgComponentMixin


class TgMenuList(QListWidget, TgComponentMixin):
    """TG菜单列表组件"""
    
    # 菜单项点击信号
    menu_item_clicked = Signal(object)
    
    def __init__(self, parent=None):
        # 初始化TG组件属性
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
        
        # 先初始化父类
        QListWidget.__init__(self, parent)
        TgComponentMixin.__init__(self)
        
        # 手动调用初始化
        self.initialize()
    
    def setup_ui(self):
        """设置用户界面"""
        # 延迟连接信号，确保对象完全初始化
        pass
    
    def apply_style(self):
        """应用样式"""
        self.setStyleSheet("""
            QListWidget {
                border: none;
                background-color: #f0f0f0;
                outline: none;
            }
            QListWidget::item {
                padding: 12px;
                border-bottom: 1px solid #e0e0e0;
                color: #333;
            }
            QListWidget::item:selected {
                background-color: #0078d4;
                color: white;
            }
            QListWidget::item:hover {
                background-color: #e5f3ff;
            }
        """)
        self._style_applied = True
    
    def add_menu_item(self, text, data=None):
        """添加菜单项"""
        item = QListWidgetItem(text)
        if data:
            item.setData(Qt.ItemDataRole.UserRole, data)
        self.addItem(item)
        return item
    
    def _on_item_clicked(self, item):
        """内部点击事件处理"""
        data = item.data(Qt.ItemDataRole.UserRole)
        self.menu_item_clicked.emit(data)
    
    def initialize(self):
        """初始化组件"""
        if not self._is_initialized:
            self.setup_ui()
            self.apply_style()
            # 在初始化完成后连接信号
            self.itemClicked.connect(self._on_item_clicked)
            self._is_initialized = True
            self._style_applied = True


class TgMenuArea(QFrame, TgComponentMixin):
    """TG菜单区域组件"""
    
    # 菜单项点击信号
    menu_item_clicked = Signal(object)
    
    def __init__(self, title="功能菜单", parent=None):
        self.title = title
        # 初始化TG组件属性
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
        
        # 先初始化父类
        QFrame.__init__(self, parent)
        TgComponentMixin.__init__(self)
        
        # 手动调用初始化
        self.initialize()
    
    def setup_ui(self):
        """设置用户界面"""
        # 创建布局
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)
        
        # 创建标题
        self.title_label = QLabel(self.title)
        self.title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.title_label.setStyleSheet("""
            QLabel {
                background-color: #e0e0e0;
                padding: 10px;
                font-weight: bold;
                font-size: 14px;
                border-bottom: 1px solid #d0d0d0;
            }
        """)
        layout.addWidget(self.title_label)
        
        # 创建菜单列表
        self.menu_list = TgMenuList()
        self.menu_list.menu_item_clicked.connect(self.menu_item_clicked.emit)
        layout.addWidget(self.menu_list)
    
    def apply_style(self):
        """应用样式"""
        self.setStyleSheet("""
            QFrame {
                background-color: #f0f0f0;
                border-right: 1px solid #d0d0d0;
            }
        """)
        self._style_applied = True
    
    def add_menu_item(self, text, data=None):
        """添加菜单项"""
        return self.menu_list.add_menu_item(text, data)
    
    def set_title(self, title):
        """设置标题"""
        self.title = title
        self.title_label.setText(title)
    
    def initialize(self):
        """初始化组件"""
        if not self._is_initialized:
            self.setup_ui()
            self.apply_style()
            self._is_initialized = True
            self._style_applied = True 